package com.pickup.utils;


import com.pickup.config.IdWorker;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class SidUtil {

    /**
     * 返回固定16位的字母数字混编的字符串。
     */
    public String nextShort() {
        long lid = new IdWorker().nextId();
        String yyMMdd = new SimpleDateFormat("yyMMdd").format(new Date());
        return yyMMdd + padLeft(encode(lid, "0123456789ABCDFGHKMNPRSTWXYZ"), 10, '0');
    }

    // 对象雪花进行编码
    public static String encode(long num, String symbols) {
        final int B = symbols.length();
        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            sb.append(symbols.charAt((int) (num % B)));
            num /= B;
        }
        return sb.reverse().toString();
    }

    // 对编码后数字如果大于10返回， 没有就补0
    public static String padLeft(String str, int size, char padChar) {
        if (str.length() >= size) return str;
        StringBuilder s = new StringBuilder();
        for (int i = size - str.length(); i > 0; --i) {
            s.append(padChar);
        }
        s.append(str);
        return s.toString();
    }

}
